package leetcode.L1000;

import java.util.PriorityQueue;

public class EX5638 {
    public static void main(String[] args) {
        int [] apples = {1,2,3,5,2};
        int [] days = {3,2,1,4,2};


        PriorityQueue<int[]> queue = new PriorityQueue<>((o1, o2) -> o1[1] < o2[1] ? -1 : 1);
        //int[0] 苹果个数，int[1]过期时间
        int eatnum = 0;
        for(int i = 0 ;i<apples.length||queue.size()>0;i++){

            while(!queue.isEmpty()){
                int[] apple = queue.peek();
                if(apple[1]<=i){
                    queue.poll();
                }
                else {
                    break;
                }
            }

            if(i<apples.length&&apples[i]>0){
                queue.add(new int[]{apples[i],days[i]+i});
            }

            int[] ap = queue.peek();
            if(ap!=null&&ap[0]>0){
                eatnum++;
                ap[0]-=1;
                if(ap[0]==0){
                    queue.poll();
                }
            }

        }


    }
}
